package zeroj.web.view.config;

import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import zeroj.web.view.FreemarkerView;

/**
 * 使用properties文件freemarker的配置
 * 
 * @author LYL(lylsir at gmail dot com)
 * 
 * @since 1.0
 *
 * 2008-9-18
 */
public class PropertyConfigFactory implements ConfigFactory {

	private static final Log log = LogFactory.getLog(PropertyConfigFactory.class);
	private String storageKey;
	private String templatePath;
	private String templateSuffix;
	
	public String getStorageKey() {
		return storageKey;
	}

	public String getTemplatePath() {
		return templatePath;
	}

	public boolean init() {
		
		String fileName = "view.properties";
		InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
    	if(is == null)
    		is = FreemarkerView.class.getClassLoader().getResourceAsStream(fileName);
    	if(is == null)
    		return false;
    	Properties props = new Properties();
       	try {
    		props.load(is);
    	} catch(Exception e) {
    		log.warn("Cannot load " + fileName, e);
    		return false;
    	}
    	setProperties(props);
    	return true;
	}
	
	public String getTemplateSuffix() {
		return templateSuffix;
	}

	private void setProperties(Properties props) {
		
		storageKey = props.getProperty("storage-key");
		templatePath = props.getProperty("template-path");
		templateSuffix = props.getProperty("template-suffix");
	}

}
